package cn.jinjimi.app.store.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import cn.jinjimi.app.entity.ZdbProduct;

import java.util.List;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.AbstractDaoSession;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.DaoConfig;

/**
 * Created by lforxeverc on 16/8/18.
 */
public class ZdbProductComDao extends AbstractDao<ZdbProduct, String> {
    public static final String TABLENAME = "zdb_common_product_table";

    /**
     * 产品对应的详情页面
     */
    public static class Properties {
        public static final Property Id = new Property(0, Integer.class, "id", true, "_id");
        public static final Property id_prod = new Property(1, String.class, "id_prod", false, "id_prod");
        public static final Property name = new Property(2, String.class, "name", false, "name");
        public static final Property tags_text = new Property(3, String.class, "tag_text", false, "tag_text");
        public static final Property descr = new Property(4, String.class, "descr", false, "descr");
        public static final Property price_title = new Property(5, String.class, "price_title", false, "price_title");
        public static final Property commission_desc = new Property(6, String.class, "commission_text", false, "commission_text");
        public static final Property icon_url = new Property(7, String.class, "icon_url", false, "icon_url");
        public static final Property cover_url = new Property(8, String.class, "cover_url", false, "cover_url");
        public static final Property detail_url = new Property(9, String.class, "view_url", false, "view_url");

        public Properties() {
        }
    }

    public ZdbProductComDao(DaoConfig config) {
        super(config);
    }

    public ZdbProductComDao(DaoConfig config, AbstractDaoSession daoSession) {
        super(config, daoSession);
    }

    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        if (db == null) return;
        String constraint = ifNotExists ? "IF NOT EXISTS " : "";
        db.execSQL("CREATE TABLE " + constraint + "\'" + TABLENAME + "\' ("
                + "\'" + Properties.Id.columnName + "\' INTEGER PRIMARY KEY AUTOINCREMENT,"
                + "\'" + Properties.id_prod.columnName + "\' TEXT,"
                + "\'" + Properties.name.columnName + "\' TEXT,"
                + "\'" + Properties.tags_text.columnName + "\' TEXT,"
                + "\'" + Properties.descr.columnName + "\' TEXT,"
                + "\'" + Properties.price_title.columnName + "\' TEXT,"
                + "\'" + Properties.commission_desc.columnName + "\' TEXT,"
                + "\'" + Properties.icon_url.columnName + "\' TEXT,"
                + "\'" + Properties.cover_url.columnName + "\' TEXT,"
                + "\'" + Properties.detail_url.columnName + "\' TEXT);");
    }

    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        if (db == null) return;
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\'" + TABLENAME + "\'";
        db.execSQL(sql);
    }

    @Override
    protected ZdbProduct readEntity(Cursor cursor, int offset) {
        ZdbProduct p = new ZdbProduct();

        p.id = (cursor.getString(offset + 1));
        p.name = (cursor.getString(offset + 2));
        p.setTagsText(cursor.getString(offset + 3));
        p.descr = (cursor.getString(offset + 4));
        p.priceTitle = cursor.getString(offset + 5);
        p.commissionDesc = cursor.getString(offset + 6);
        p.iconUrl = (cursor.getString(offset + 7));
        p.coverUrl = (cursor.getString(offset + 8));
        p.detailUrl = (cursor.getString(offset + 9));
        return p;
    }

    @Override
    protected String readKey(Cursor cursor, int offset) {
        return cursor.getString(offset + 0);
    }

    @Override
    protected void readEntity(Cursor cursor, ZdbProduct entity, int offset) {
        entity.id = (cursor.getString(offset + 1));
        entity.name = (cursor.getString(offset + 2));
        entity.setTagsText(cursor.getString(offset + 3));
        entity.descr = (cursor.getString(offset + 4));
        entity.priceTitle = (cursor.getString(offset + 5));
        entity.commissionDesc = (cursor.getString(offset + 6));
        entity.iconUrl = (cursor.getString(offset + 7));
        entity.coverUrl = (cursor.getString(offset + 8));
        entity.detailUrl = (cursor.getString(offset + 9));

    }

    @Override
    protected void bindValues(SQLiteStatement stmt, ZdbProduct entity) {
        stmt.clearBindings();

        String id = entity.id;
        if (id != null) {
            stmt.bindString(2, id);
        }

        String name = entity.name;
        if (name != null) {
            stmt.bindString(3, name);
        }

        String tag = entity.getTagsText();
        if (tag != null) {
            stmt.bindString(4, tag);
        }

        String descr = entity.descr;
        if (descr != null) {
            stmt.bindString(5, descr);
        }

        String price = entity.priceTitle;
        if (price != null) {
            stmt.bindString(6, price);
        }

        String commissiondescr = entity.commissionDesc;
        if (commissiondescr != null) {
            stmt.bindString(7, commissiondescr);
        }

        String iconUrl = entity.iconUrl;
        if (iconUrl != null) {
            stmt.bindString(8, iconUrl);
        }
        String coverUrl = entity.coverUrl;
        if (coverUrl != null) {
            stmt.bindString(9, coverUrl);
        }
        String viewUrl = entity.detailUrl;
        if (viewUrl != null) {
            stmt.bindString(10, viewUrl);
        }
    }

    @Override
    protected String updateKeyAfterInsert(ZdbProduct entity, long rowId) {
        return null;
    }

    @Override
    protected String getKey(ZdbProduct entity) {
        return null;
    }

    @Override
    protected boolean isEntityUpdateable() {
        return true;
    }

    public void saveAll(List<ZdbProduct> list) {
        deleteAll();
        insertInTx(list);
    }
}
